package cn.btks.resultintegration.achievements.repository;

import cn.btks.resultintegration.achievements.pojo.EnvironmentAttribute;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @Author lijun
 * @Date 2021/1/27 11:23
 */
@Repository
public interface EnvironmentAttributeRepository extends JpaRepository<EnvironmentAttribute,Long>, JpaSpecificationExecutor<EnvironmentAttribute> {

    /**
     * 根据成果ID查询
     * @param catalogId
     * @return
     */
    List<EnvironmentAttribute> findAllByCatalogId(Long catalogId);

    /**
     * 根据成果ID及师名称查询
     * @param catalogId
     * @return
     */
    List<EnvironmentAttribute> findAllByCatalogIdAndCITY(Long catalogId,String city);

    /**
     * 根据成果ID及团场名称查询
     * @param catalogId
     * @param county
     * @return
     */
    List<EnvironmentAttribute> findAllByCatalogIdAndCOUNTY(Long catalogId,String county);

    /**
     * 根据成果ID删除属性信息
     * @param achievementId
     * @return
     */
    @Modifying
    @Query(value = "delete from environment_attribute where achievement_id = ?1",nativeQuery = true)
    int deleteByAchievementId(Long achievementId);

    /***********统计查询***********/

    /**
     * 统计查询环境管控单元面积和数量
     * @return
     */
    @Query(value = "select SUM(CASE WHEN ea.catalog_id = ?1 THEN ea.shape_area ELSE 0 END) AS firstArea,SUM(CASE WHEN ea.catalog_id = ?2 THEN ea.shape_area ELSE 0 END) AS secondArea,SUM(CASE WHEN ea.catalog_id = ?3 THEN ea.shape_area ELSE 0 END) AS thirdArea,SUM(CASE WHEN ea.catalog_id = ?1 THEN 1 ELSE 0 END) AS firstCount,SUM(CASE WHEN ea.catalog_id = ?2 THEN 1 ELSE 0 END) AS secondCount,SUM(CASE WHEN ea.catalog_id = ?3 THEN 1 ELSE 0 END) AS thirdCount from environment_attribute ea",nativeQuery = true)
    Map<String,Object> queryUnitAreaAndCount(Long firstId,Long secondId,Long thirdId);

    /**
     * 查询各个师的管控单元的数量
     * @return
     */
    @Query(value = "SELECT ea.city as city, SUM(CASE WHEN ea.catalog_id = ?1 THEN 1 ELSE 0 END) AS firstCount,SUM(CASE WHEN ea.catalog_id = ?2 THEN 1 ELSE 0 END) AS secondCount,SUM(CASE WHEN ea.catalog_id = ?3 THEN 1 ELSE 0 END) AS thirdCount FROM environment_attribute ea GROUP BY city",nativeQuery = true)
    List<Map<String, Object>> queryCityUnitCount(Long firstId,Long secondId,Long thirdId);

    /**
     * 查询各个师的管控单元的面积和数量
     * @return
     */
    @Query(value = "SELECT ea.city as city, SUM(CASE WHEN ea.catalog_id = ?2 THEN ea.shape_area ELSE 0 END) AS firstArea,SUM(CASE WHEN ea.catalog_id = ?3 THEN ea.shape_area ELSE 0 END) AS secondArea,SUM(CASE WHEN ea.catalog_id = ?4 THEN ea.shape_area ELSE 0 END) AS thirdArea,SUM(CASE WHEN ea.catalog_id = ?2 THEN 1 ELSE 0 END) AS firstCount,SUM(CASE WHEN ea.catalog_id = ?3 THEN 1 ELSE 0 END) AS secondCount,SUM(CASE WHEN ea.catalog_id = ?4 THEN 1 ELSE 0 END) AS thirdCount FROM environment_attribute ea WHERE city = ?1",nativeQuery = true)
    Map<String, Object> queryCityUnitAreaAndCount(String city,Long firstId,Long secondId,Long thirdId);

    /**
     * 查询各个团场的管控单元的面积和数量
     * @return
     */
    @Query(value = "SELECT ea.county as county, SUM(CASE WHEN ea.catalog_id = ?2 THEN ea.shape_area ELSE 0 END) AS firstArea,SUM(CASE WHEN ea.catalog_id = ?3 THEN ea.shape_area ELSE 0 END) AS secondArea,SUM(CASE WHEN ea.catalog_id = ?4 THEN ea.shape_area ELSE 0 END) AS thirdArea,SUM(CASE WHEN ea.catalog_id = ?2 THEN 1 ELSE 0 END) AS firstCount,SUM(CASE WHEN ea.catalog_id = ?3 THEN 1 ELSE 0 END) AS secondCount,SUM(CASE WHEN ea.catalog_id = ?4 THEN 1 ELSE 0 END) AS thirdCount FROM environment_attribute ea WHERE county = ?1",nativeQuery = true)
    Map<String, Object> queryCountyUnitAreaAndCount(String county,Long firstId,Long secondId,Long thirdId);

    /**
     * 统计查询师下的团的管控单元的数量
     * @param city
     * @param firstId
     * @param secondId
     * @param thirdId
     * @return
     */
    @Query(value = "SELECT ea.city as city,ea.county as county, SUM(CASE WHEN ea.catalog_id = ?2 THEN 1 ELSE 0 END) AS firstCount,SUM(CASE WHEN ea.catalog_id = ?3 THEN 1 ELSE 0 END) AS secondCount,SUM(CASE WHEN ea.catalog_id = ?4 THEN 1 ELSE 0 END) AS thirdCount FROM `environment_attribute` ea WHERE city = ?1  GROUP BY city,county",nativeQuery = true)
    List<Map<String,Object>> queryCountyUnitCount(String city,Long firstId,Long secondId,Long thirdId);

    /**
     * 统计查询团场的管控单元的数量
     * @param county
     * @param firstId
     * @param secondId
     * @param thirdId
     * @return
     */
    @Query(value = "SELECT ea.county as county, SUM(CASE WHEN ea.catalog_id = ?2 THEN 1 ELSE 0 END) AS firstCount,SUM(CASE WHEN ea.catalog_id = ?3 THEN 1 ELSE 0 END) AS secondCount,SUM(CASE WHEN ea.catalog_id = ?4 THEN 1 ELSE 0 END) AS thirdCount FROM `environment_attribute` ea WHERE county = ?1  GROUP BY county",nativeQuery = true)
    List<Map<String,Object>> queryCountyUnitCountOnly(String county,Long firstId,Long secondId,Long thirdId);
}
